﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;

namespace CatUtils.Utils.SpecialUtils.View
{
    /// <summary>
    /// 记忆界面信息
    /// </summary>
    public class ViewInfoMemory
    {
        /// <summary>
        ///界面类型名字
        /// </summary>
        public struct ViewInfoName
        {
            /// <summary>
            /// TextBox Name
            /// </summary>
            public static string TextBox { get; } = "Textbox";

            /// <summary>
            /// Combobox Name
            /// </summary>
            public static string Combobox { get; } = "Combobox";

            /// <summary>
            /// CheckBox Name
            /// </summary>
            public static string CheckBox { get; } = "CheckBox";

            /// <summary>
            /// Lable Name
            /// </summary>
            public static string Label { get; } = "Lable";

            /// <summary>
            ///  RichText Name
            /// </summary>
            public static string RichTextBox { get; } = "RichTextBox";
        }

        /// <summary>
        /// 保存控件信息
        /// </summary>
        /// <param name="ControlName"></param>
        /// <param name="ControlType"></param>
        /// <param name="SaveValue"></param>
        public void SaveControlInfo(string ControlName, string ControlType, string SaveValue)
        {
            string FileName = CreateFilePath(ControlName, ControlType);
            FileHelper.WriteText(FileName, SaveValue);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="textBox"></param>
        public void SaveTextboxInfo(TextBox textBox)
        {
            SaveTextboxInfo(textBox.Name, textBox.Text);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="TextBoxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveTextboxInfo(string TextBoxName, string SaveValue)
        {
            SaveControlInfo(TextBoxName, ViewInfoName.TextBox, SaveValue);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="textBox"></param>
        /// <returns></returns>
        public async Task InitTextboxSaveInfo(TextBox textBox)
        {
            await InitTextboxSaveInfo(textBox, textBox.Name);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="TextboxName"></param>
        public async Task InitTextboxSaveInfo(TextBox textBox, string TextboxName)
        {
            string FileName = CreateFilePath(TextboxName, ViewInfoName.TextBox);
            string Content = FileHelper.ReadAllText(FileName);

            await new ViewAsyncUpdate().TextBoxUpdateText(textBox, Content);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="richTextBox"></param>
        public void SaveRichTextboxInfo(RichTextBox richTextBox)
        {
            SaveRichTextboxInfo(richTextBox.Name, richTextBox.Text);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="RichTextboxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveRichTextboxInfo(string RichTextboxName, string SaveValue)
        {
            SaveControlInfo(RichTextboxName, ViewInfoName.RichTextBox, SaveValue);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="RichTextbox"></param>
        /// <returns></returns>
        public async Task InitRichTextInfo(RichTextBox RichTextbox)
        {
            await InitRichTextInfo(RichTextbox, RichTextbox.Name);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="RichTextbox"></param>
        /// <param name="RichTextBoxName"></param>
        public async Task InitRichTextInfo(RichTextBox RichTextbox, string RichTextBoxName)
        {
            string FileName = CreateFilePath(RichTextBoxName, ViewInfoName.TextBox);
            string Content = FileHelper.ReadAllText(FileName);

            await new ViewAsyncUpdate().RichTextBoxUpdateText(RichTextbox, Content);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="label"></param>
        public void SaveLableInfo(Label label)
        {
            SaveLableInfo(label.Name, label.Text);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="TextBoxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveLableInfo(string TextBoxName, string SaveValue)
        {
            SaveControlInfo(TextBoxName, ViewInfoName.Label, SaveValue);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="label"></param>
        /// <param name="TextboxName"></param>
        /// <returns></returns>
        public async Task InitLableSaveInfo(Label label)
        {
            await InitLableSaveInfo(label, label.Name);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="label"></param>
        /// <param name="TextboxName"></param>
        /// <returns></returns>
        public async Task InitLableSaveInfo(Label label, string TextboxName)
        {
            string FileName = CreateFilePath(TextboxName, ViewInfoName.Label);
            string Content = FileHelper.ReadAllText(FileName);

            await new ViewAsyncUpdate().LableUpdateTextAsync(label, Content);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="checkBox"></param>
        public void SaveCheckboxInfo(CheckBox checkBox)
        {
            SaveCheckboxInfo(checkBox.Name, checkBox.Checked);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="CheckBoxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveCheckboxInfo(string CheckBoxName, bool SaveValue)
        {
            SaveControlInfo(CheckBoxName, ViewInfoName.CheckBox, SaveValue.ToString());
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="CheckBoxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveCheckboxInfo(string CheckBoxName, string SaveValue)
        {
            SaveControlInfo(CheckBoxName, ViewInfoName.CheckBox, SaveValue);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="checkBox"></param>
        /// <returns></returns>
        public async Task InitCheckboxSaveInfo(CheckBox checkBox)
        {
            await InitCheckboxSaveInfo(checkBox, checkBox.Name);
        }

        /// <summary>
        /// 获取选择器保存信息
        /// </summary>
        /// <param name="checkBox"></param>
        /// <param name="CheckBoxName"></param>
        public async Task InitCheckboxSaveInfo(CheckBox checkBox, string CheckBoxName)
        {
            string FileName = CreateFilePath(CheckBoxName, ViewInfoName.CheckBox);
            string Content = FileHelper.ReadAllText(FileName);
            if (Content == "True")
                await new ViewAsyncUpdate().CheckBoxChecked(checkBox, true);
            else
                await new ViewAsyncUpdate().CheckBoxChecked(checkBox, false);
        }

        /// <summary>
        /// 保存需要记住的信息文件
        /// </summary>
        /// <param name="comboBox"></param>
        public void SaveComboBoxInfo(ComboBox comboBox)
        {
            SaveComboBoxInfo(comboBox.Name, comboBox.Text);
        }

        /// <summary>
        /// 保存需要记住的信息文件。
        /// </summary>
        /// <param name="ComboBoxName"></param>
        /// <param name="SaveValue"></param>
        public void SaveComboBoxInfo(string ComboBoxName, string SaveValue)
        {
            SaveControlInfo(ComboBoxName, ViewInfoName.Combobox, SaveValue);
        }

        /// <summary>
        /// 保存需要记住的信息文件  如果保存值SaveValue包含JumpTextFlag，则不保存。
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="JumpTextFlag"></param>
        public void SaveComboBoxInfo(ComboBox comboBox, string JumpTextFlag)
        {
            SaveComboBoxInfo(comboBox.Name, comboBox.Text, JumpTextFlag);
        }

        /// <summary>
        /// 保存需要记住的信息文件  如果保存值SaveValue包含JumpTextFlag，则不保存。
        /// </summary>
        /// <param name="ComboBoxName"></param>
        /// <param name="SaveValue"></param>
        /// <param name="JumpTextFlag"> 如果保存值SaveValue包含JumpTextFlag，则不保存</param>
        public void SaveComboBoxInfo(string ComboBoxName, string SaveValue, string JumpTextFlag)
        {
            if (SaveValue != "" && SaveValue.IndexOf(JumpTextFlag) >= 0)
                return;
            SaveControlInfo(ComboBoxName, ViewInfoName.Combobox, SaveValue);
        }

        /// <summary>
        /// 获取保存的信息列表
        /// </summary>
        /// <param name="comboBox"></param>
        /// <returns></returns>
        public async Task InitComboboxSaveInfo(ComboBox comboBox)
        {
            await InitComboboxSaveInfo(comboBox, comboBox.Name);
        }

        /// <summary>
        /// 获取保存的信息列表
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="ComboBoxName"></param>
        public async Task InitComboboxSaveInfo(ComboBox comboBox, string ComboBoxName)
        {
            string FileName = CreateFilePath(ComboBoxName, ViewInfoName.Combobox);
            string Content = FileHelper.ReadAllText(FileName);

            if (Content == "")
            {
                return;
            }
            int ComboBoxIndex = 0;
            foreach (var item in comboBox.Items)//这一步操作是为了触发Combobox的SelectChanged事件
            {
                if (item.ToString() == Content)
                {
                    break;
                }
                ComboBoxIndex++;
            }
            if (ComboBoxIndex < comboBox.Items.Count)
                await new ViewAsyncUpdate().ComboBoxSelectedIndex(comboBox, ComboBoxIndex);
            else
                await new ViewAsyncUpdate().ComboBoxUpdateText(comboBox, Content);
        }

        /// <summary>
        /// 创建信息文件的路径
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="FileType"></param>
        /// <returns></returns>
        public string CreateFilePath(string FileName, string FileType)
        {
            return StaticValue.BinViewPath + $"{FileName}.{FileType}";
        }

        /// <summary>
        /// 获取保存的界面值
        /// </summary>
        /// <param name="ViewName">界面名字</param>
        /// <param name="ViewType">类型请通过：ViewInfoName 进行获取。</param>
        /// <returns></returns>
        public string GetmemoryInfo(string ViewName, string ViewType)
        {
            string FileName = CreateFilePath(ViewName, ViewType);
            return FileHelper.ReadAllText(FileName);
        }
    }
}